<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_News_Models_Mysql_Cron extends Modules_News_Models_Cron
{	
	protected $_table = 'news_cron';
	
	public function getById($cronId)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table . '
							WHERE cron_id = "' . $this->_conn->escape($cronId) . '" LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == $this->_conn->count($rs)) ? null : $this->_conn->fetchAll($rs);
		$this->_conn->freeResult($rs);
		return $return;
	}
	
	public function getAllCrons($status = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . $this->_table;
		if ($status) {
			$sql .= ' WHERE status = "' . $this->_conn->escape($status) . '"';
		}
		$sql .= ' ORDER BY name ASC';
		$rs = $this->_conn->query($sql);
		$crons = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$crons[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $crons;
	}
	
	public function add($cron)
	{
		return $this->_conn->insert($cron, $this->_conn->_tablePrefix . $this->_table);	
	}
	
	public function update($cron)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . ' 
					SET website_name = "%s", name = "%s", status = "%s", category_ids = "%s",
					source = "%s", website = "%s", frequency = "%s", priority = "%s", last_update = "%s"
					WHERE cron_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($cron['website_name']),
							$this->_conn->escape($cron['name']),
							$this->_conn->escape($cron['status']),
							$this->_conn->escape($cron['category_ids']),
							$this->_conn->escape($cron['source']),
							$this->_conn->escape($cron['website']),
							$this->_conn->escape($cron['frequency']),
							$this->_conn->escape($cron['priority']),
							$this->_conn->escape($cron['last_update']),
							$this->_conn->escape($cron['cron_id']));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();	
	}
	
	public function delete($cronId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE cron_id = "' . $this->_conn->escape($cronId) . '"';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function find($offset = null, $limit = null, $condition = array())
	{
		$sql = 'SELECT c.*, w.name AS web_name FROM ' . $this->_conn->_tablePrefix . $this->_table . ' AS c
				LEFT JOIN ' . $this->_conn->_tablePrefix . 'news_web AS w
					ON c.website_name = w.web_id
				WHERE 1';
		if (isset($condition['keyword']) && $condition['keyword']) {
			$sql .= ' AND c.name LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
		}
		if (isset($condition['web_id']) && $condition['web_id']) {
			$sql .= ' AND c.website_name = "' . $this->_conn->escape($condition['web_id']) . '"';
		}
		
		$sql .= ' ORDER BY cron_id DESC';
		if ($offset !== false && $limit !== false) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}
		
		$rs = $this->_conn->query($sql);
		$crons = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$crons[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $crons;
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(*) as num_crons FROM ' . $this->_conn->_tablePrefix . $this->_table . ' AS c 
				LEFT JOIN ' . $this->_conn->_tablePrefix . 'news_web AS w
					ON c.website_name = w.web_id
				WHERE 1';
		if (isset($condition['keyword']) && $condition['keyword']) {
			$sql .= ' AND c.name LIKE "%' . $this->_conn->escape($condition['keyword']) . '%"';
		}
		if (isset($condition['web_id']) && $condition['web_id']) {
			$sql .= ' AND c.website_name = "' . $this->_conn->escape($condition['web_id']) . '"';
		}
		$sql .= ' LIMIT 1';
		
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_crons'];
	}
	
	public function updateStatus($cronId, $status)
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . ' 
					SET status = "%s" 
					WHERE cron_id = "%s"';
		$sql = sprintf($sql, $this->_conn->escape($status),
							$this->_conn->escape($cronId));
		$rs = $this->_conn->query($sql);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
	
	public function lock($sql)
	{
		if (null == $sql) {
			return;
		}
		$stringId = null;
		$recordSet = $this->_conn->query($sql);
		
		while($row = $this->_conn->fetchAll($recordSet)) {
			$stringId .= (null == $stringId) ? '' : ',';
			$stringId .= $row['cron_id'];
		}
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . ' 
					SET is_locked = "1" 
					WHERE cron_id IN (%s)';
		$sql = sprintf($sql, $this->_conn->escape($stringId));
		$rs = $this->_conn->query($sql);
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
	
	public function unlock()
	{
		$sql = 'UPDATE ' . $this->_conn->_tablePrefix . $this->_table . ' 
					SET is_locked = "0" 
					WHERE is_locked = 1';
		$rs = $this->_conn->query($sql);
		$this->_conn->freeResult($rs);
		return $this->_conn->affectedRows();
	}
}